package com.zhl.arrays4;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

import static java.math.BigDecimal.valueOf;

public class ArraysTest1 {
    public static void main(String[] args) {
        //目标：掌握Arrays类的常用方法
        //1、public static String toString(类型[] a)  将数组转换为字符串,返回数组的内容
        int arr[] = {1,2,3,4,5,6,7,8,9,10};
        String str = Arrays.toString(arr);
        System.out.println(str);

        //2、public static int[] copyOfRange(类型[] arr,起始索引,结束索引)   拷贝数组,指定范围
        int[] newArr = Arrays.copyOfRange(arr, 2, 7); // 3,4,5,6,7
        System.out.println( Arrays.toString(newArr));


        //3、public static int[] copyOf(类型[] arr,int newLength)   拷贝数组,指定长度
        int[] arr3 = Arrays.copyOf(arr,16);
        System.out.println( Arrays.toString(arr3));


        //4、public static setAll(类型[] arr,IntToXFunction generator)   把数组中的原数据改成新数据然后存进去
        double prices[] = {10.5, 20.3, 15.6, 8.9, 12.7};
        //把所有的价格打八折存进去
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                BigDecimal bd = valueOf(prices[value]);
                BigDecimal result = bd.multiply(valueOf(0.8));
                return result.doubleValue();  //将BigDecimal转换为double类型并返回。
            }
        });
        System.out.println(Arrays.toString(prices));
        //Arrays.setAll(prices, i -> prices[i] * 0.8);


        //5、public static sort(类型[] arr)   对数组进行排序(默认升序排序)
        Arrays.sort(prices);
        System.out.println(Arrays.toString(prices));

    }
}
